package cn.xny.product.service.impl;

import cn.xny.basic.service.BaiduAiAuditService;
import cn.xny.basic.util.AjaxResult;
import cn.xny.pet.domain.Pet;
import cn.xny.pet.domain.PetDetail;
import cn.xny.pet.domain.PetOnlineAuditLog;
import cn.xny.product.domain.Product;

import cn.xny.basic.service.impl.BaseServiceImpl;
import cn.xny.product.domain.ProductDetail;
import cn.xny.product.domain.ProductOnlineAuditLog;
import cn.xny.product.mapper.ProductDetailMapper;
import cn.xny.product.mapper.ProductMapper;
import cn.xny.product.mapper.ProductOnlineAuditLogMapper;
import cn.xny.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {


    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private BaiduAiAuditService baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;


    @Transactional
    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){

            productDetail.setProductId(product.getId());
            //添加宠物详情
            productDetailMapper.save(productDetail);
        }


    }
    @Transactional
    @Override
    public void del(Long id) {
        //删除宠物详情通过宠物id
        productDetailMapper.delProductIdById(id);

        super.del(id);
    }
    @Transactional
    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if(productDetail!=null){
            productDetail.setProductId(product.getId());
            //修改宠物详情通过宠物id
            productDetailMapper.updateByProductId(productDetail);

        }


    }
    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }


    @Transactional
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //先获取每个对象
            Product product = productMapper.loadByAll(id);

            //审核名字与图片
            Boolean Bool1 = baiduAiAuditService.textAudit(product.getName());
            Boolean Bool2 = baiduAiAuditService.imageAudit(product.getResources());
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            if(Bool1 && Bool2){
                productMapper.onSale(id,new Date());

                log.setState(1);
                log.setNote("审核成功");

                productOnlineAuditLogMapper.save(log);


            }else {

                log.setState(0);
                log.setNote("审核失败");

                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");

            }



        }

        return new AjaxResult();


    }
    @Transactional
    //下架
    @Override
    public void offsale(List<Long> ids) {

        productMapper.offSale(ids,new Date());

    }

}
